package com.zw.cloud.influxdb.enums;

import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Getter
public enum StenterInfluxDBEnum {

    SPEED("speed", "车速","speedSetting",null),

    DRYING_ROOM_ACTUAL_TEMP("dryingRoomActualTemp", "烘箱温度","dryingRoomPresetTemp",12),

    WIND_SPEED("windSpeed", "排风转速","speciWindSpeed",8),

    CYCLE_WIND_SPEED("cycleWindSpeed", "循环风转速","speciCycleWindSpeed",12),

    TOTAL_AMPLITUDE("totalAmplitude", "总门幅","totalDoorWidthSetting",null),

    FRONT_DOOR_WIDTH_ACTUAL("frontDoorWidthActual", "前门幅","frontDoorWidthSetting",null),

    REAR_DOOR_WIDTH_ACTUAL("rearDoorWidthActual", "后门幅","rearDoorWidthSetting",null),

    DOOR_WIDTH_ACTUAL("doorWidthActual", "门幅实际","doorWidthSetting",12),

    TOP_FEED("topFeed", "上超喂",null,null),

    LOWER_FEED("lowerFeed", "下超喂",null,null),

    ;

    private String actKey;
    private String remark;
    private String settingKey;
    private Integer maxCount;

    StenterInfluxDBEnum(String actKey, String remark, String settingKey, Integer maxCount) {
        this.actKey = actKey;
        this.remark = remark;
        this.settingKey = settingKey;
        this.maxCount = maxCount;
    }

    public static List<KeyAndSettingKey> getKeyAndSettingKey(String key) {
        StenterInfluxDBEnum stenterInfluxDBEnum = StenterInfluxDBEnum.valueOf(key);
        Integer count = stenterInfluxDBEnum.getMaxCount();
        if (Objects.isNull(count)) {
            return Lists.newArrayList(new KeyAndSettingKey(stenterInfluxDBEnum.getActKey(), stenterInfluxDBEnum.getSettingKey()));
        }
        ArrayList<KeyAndSettingKey> keys = new ArrayList<>(count);
        for (int i = 1; i <= count; i++) {
            keys.add(new KeyAndSettingKey(stenterInfluxDBEnum.getActKey() + i, stenterInfluxDBEnum.getSettingKey() + i));
        }
        return keys;
    }

    public static List<KeyAndSettingKey> getAllKeyAndSettingKey() {
        List<StenterInfluxDBEnum.KeyAndSettingKey> keyAndSettingKey = StenterInfluxDBEnum.getKeyAndSettingKey(StenterInfluxDBEnum.SPEED.name());
        keyAndSettingKey.addAll(StenterInfluxDBEnum.getKeyAndSettingKey(StenterInfluxDBEnum.DRYING_ROOM_ACTUAL_TEMP.name()));
        keyAndSettingKey.addAll(StenterInfluxDBEnum.getKeyAndSettingKey(StenterInfluxDBEnum.WIND_SPEED.name()));
        keyAndSettingKey.addAll(StenterInfluxDBEnum.getKeyAndSettingKey(StenterInfluxDBEnum.CYCLE_WIND_SPEED.name()));
        keyAndSettingKey.addAll(StenterInfluxDBEnum.getKeyAndSettingKey(StenterInfluxDBEnum.TOTAL_AMPLITUDE.name()));
        keyAndSettingKey.addAll(StenterInfluxDBEnum.getKeyAndSettingKey(StenterInfluxDBEnum.FRONT_DOOR_WIDTH_ACTUAL.name()));
        keyAndSettingKey.addAll(StenterInfluxDBEnum.getKeyAndSettingKey(StenterInfluxDBEnum.REAR_DOOR_WIDTH_ACTUAL.name()));
        keyAndSettingKey.addAll(StenterInfluxDBEnum.getKeyAndSettingKey(StenterInfluxDBEnum.DOOR_WIDTH_ACTUAL.name()));
        keyAndSettingKey.addAll(StenterInfluxDBEnum.getKeyAndSettingKey(StenterInfluxDBEnum.TOP_FEED.name()));
        keyAndSettingKey.addAll(StenterInfluxDBEnum.getKeyAndSettingKey(StenterInfluxDBEnum.LOWER_FEED.name()));
        return keyAndSettingKey;
    }


    /**
     * 同一节数据取平均数
     * 上超喂 下超喂
     */
    public static List<KeyAndSettingKey> getAvgKeyAndSettingKey() {
        List<StenterInfluxDBEnum.KeyAndSettingKey> keyAndSettingKey = StenterInfluxDBEnum.getKeyAndSettingKey(StenterInfluxDBEnum.TOP_FEED.name());
        keyAndSettingKey.addAll(StenterInfluxDBEnum.getKeyAndSettingKey(StenterInfluxDBEnum.LOWER_FEED.name()));
        return keyAndSettingKey;
    }

    /**
     * 取相同最多的值(如相同值有多项，取最大值)
     * 烘箱温度 排风转速 循环风转速 门幅
     */
    public static List<KeyAndSettingKey> geMaxSameKeyAndSettingKey() {
        List<StenterInfluxDBEnum.KeyAndSettingKey> keyAndSettingKey = StenterInfluxDBEnum.getKeyAndSettingKey(StenterInfluxDBEnum.DRYING_ROOM_ACTUAL_TEMP.name());
        keyAndSettingKey.addAll(StenterInfluxDBEnum.getKeyAndSettingKey(StenterInfluxDBEnum.WIND_SPEED.name()));
        keyAndSettingKey.addAll(StenterInfluxDBEnum.getKeyAndSettingKey(StenterInfluxDBEnum.CYCLE_WIND_SPEED.name()));
        keyAndSettingKey.addAll(StenterInfluxDBEnum.getKeyAndSettingKey(StenterInfluxDBEnum.TOTAL_AMPLITUDE.name()));
        keyAndSettingKey.addAll(StenterInfluxDBEnum.getKeyAndSettingKey(StenterInfluxDBEnum.FRONT_DOOR_WIDTH_ACTUAL.name()));
        keyAndSettingKey.addAll(StenterInfluxDBEnum.getKeyAndSettingKey(StenterInfluxDBEnum.REAR_DOOR_WIDTH_ACTUAL.name()));
        keyAndSettingKey.addAll(StenterInfluxDBEnum.getKeyAndSettingKey(StenterInfluxDBEnum.DOOR_WIDTH_ACTUAL.name()));
        return keyAndSettingKey;
    }

    /**
     * 连续出现2次(含)以上数值中的最大值，如有效时间段内无连续值，则取最大值；精确到整数
     * 温度
     */
    public static List<KeyAndSettingKey> getConsecutiveDigitsKeyAndSettingKey() {
        return StenterInfluxDBEnum.getKeyAndSettingKey(StenterInfluxDBEnum.SPEED.name());
    }

    public static void main(String[] args) {

        System.out.println(JSON.toJSONString(getAllKeyAndSettingKey()));
    }

    @Getter
    @AllArgsConstructor
    public static class KeyAndSettingKey {
        private String actKey;
        private String settingKey;
    }
}
